package com.studentmanage.manage_student.service;

import com.alibaba.fastjson.JSONObject;
import com.studentmanage.framework.domain.base.*;
import com.studentmanage.framework.domain.base.request.QueryStudentRequest;
import com.studentmanage.framework.domain.base.response.StudentCode;
import com.studentmanage.framework.domain.common.response.CommonCode;
import com.studentmanage.framework.domain.common.response.QueryResult;
import com.studentmanage.framework.domain.common.response.ResponseResult;
import com.studentmanage.framework.domain.common.response.ResponseValue;
import com.studentmanage.framework.utils.PageSizeUtil;
import com.studentmanage.manage_student.client.ClassClient;
import com.studentmanage.manage_student.client.CollegeClient;
import com.studentmanage.manage_student.dao.StudentExtMapper;
import com.studentmanage.manage_student.dao.StudentRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class StudentService {
    @Autowired
    private StudentRepository studentRepository;
    @Autowired(required = false)
    private StudentExtMapper studentExtMapper;
    @Autowired
    private ClassClient classClient;
    @Autowired
    private CollegeClient collegeClient;

    //查询学生列表(分页+条件查询)
    public QueryResult findStudentList(int page, int size, QueryStudentRequest queryStudentRequest) {
        queryStudentRequest.setPage(PageSizeUtil.formatPage(page,size));
        queryStudentRequest.setSize(PageSizeUtil.formatSize(size));
        List<StudentExt> studentList = studentExtMapper.findStudentList(queryStudentRequest);
        QueryResult queryResult = new QueryResult();
        Integer total = studentExtMapper.getTotalNum(queryStudentRequest);
        queryResult.setTotal(total);
        queryResult.setList(studentList);
        return queryResult;
    }

    //添加学生信息
    public ResponseResult addStudent(Student student){
        //判断是否存在班级，学院，专业
        if(student == null || student.getClassId() == null
            || student.getEntranceTime() == null){
            return new ResponseResult(StudentCode.CLASS_IS_NULL);
        }
        if(StringUtils.isEmpty(student.getStatus())){
            student.setStatus("1");
        }
        //远程调用查询班级，专业，学院
        ResponseValue<Clas> responseValue1 = classClient.findByCollegeId(student.getClassId());
        if(!responseValue1.isSuccess()){
            return new ResponseResult(StudentCode.CLASS_IS_NOT_EXIST);
        }
        Clas clas = responseValue1.getT();
        ResponseValue<Specialize> responseValue2 = collegeClient.findSpecializeById(clas.getSpecializeId());
        Specialize specialize = responseValue2.getT();
        ResponseValue<College> responseValue3 = collegeClient.findCollegeById(specialize.getCollegeId());
        College college = responseValue3.getT();
        //生成学号
        String classCode = clas.getClassCode();
        Integer num = clas.getNum() + 1;
        String classNum;
        if(num < 10){
            classNum = "0" + num.toString();
        }else{
            classNum = num.toString();
        }
        String specializeCode = specialize.getSpecializeCode();
        String collegeCode = college.getCollegeCode();
        String entranceTime = student.getEntranceTime();
        String grade = entranceTime.substring(2,4);
        String status = null;
        if(student.getStatus().equals("本科"))
            status = "1";
        else
            status = "2";
        String id = grade + status + collegeCode + classCode + specializeCode + classNum;
        student.setId(id);
        //修改入学时间
        student.setEntranceTime(entranceTime.substring(0,4));
        student.setGrade(entranceTime.substring(0,4));
        studentRepository.save(student);
        //班级人数+1
        clas.setNum(num);
        classClient.updateClass(clas);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //根据学号查询学生信息
    public ResponseValue<Student> findStudentById(String id) {
        if(StringUtils.isEmpty(id)){
            return new ResponseValue<Student>(false, null);
        }
        Optional<Student> optional = studentRepository.findById(id);
        if(!optional.isPresent()){
            return new ResponseValue<Student>(false, null);
        }
        Student student = optional.get();
        return new ResponseValue<Student>(true, student);
    }

    //批量删除
    public ResponseResult delStudent(String[] ids) {
        if(ids == null){
            return new ResponseResult(StudentCode.DELETE_LIST_NULL);
        }
        for(String id : ids){
            Optional<Student> optional = studentRepository.findById(id);
            if(optional.isPresent()){
                studentRepository.delete(optional.get());
            }
        }
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //修改学生
    public ResponseResult editStudent(Student student) {
        //修改入学时间
        String entranceTime = student.getEntranceTime();
        student.setEntranceTime(entranceTime.substring(0,4));
        studentRepository.save(student);
        return new ResponseResult(CommonCode.SUCCESS);
    }
}
